import { Component } from '@angular/core';
import { NavParams, NavController } from 'ionic-angular';

import { HistoryPage } from "../history/history";
import { SettlementPage } from "../settlement/settlement";
import { SetCheckInPage } from "../setCheckIn/setCheckIn";
import { SetCheckOutPage } from "../setCheckOut/setCheckOut";

import { StorageService } from '../../providers/StorageService';
import { RoomService } from '../../providers/RoomService';
import { CheckOutService } from '../../providers/CheckOutService';

@Component({
  selector: 'page-roomDetail',
  templateUrl: 'roomDetail.html',
  providers: [RoomService, CheckOutService]
})
export class RoomDetailPage {
  room: any;
  room_id: number;
  room_name: string;

  constructor(
    navParams: NavParams,
    private navCtrl: NavController,
    private storageService: StorageService,
    private roomService: RoomService
  ) {
    this.room_name = navParams.get('room_name');
    this.room_id = navParams.get('room_id');

    this.room = {
      'room_name': '',
      'build_address' : '',
      'check_in_contact': '',
      'check_in_mobile': '',
      'check_in_date': '',
      'check_in_status': '0',
      'check_in_status_name': '未入住'
    };

    this.roomService.get({'room_id': this.room_id}).then(data => {
      this.room.room_name = data.room.room_name;
      this.room.build_address = data.room.build_address;

      this.storageService.write('RoomInfo', data.room);

      if (data.check_in) {
        this.room.check_in_id = data.check_in.check_in_id;
        this.room.check_in_contact = data.check_in.check_in_contact;
        this.room.check_in_mobile = data.check_in.check_in_mobile;
        this.room.check_in_date = data.check_in.check_in_date;
        this.room.check_in_status = data.check_in.check_in_status;
        this.room.check_in_status_name = data.check_in.check_in_status == '1' ? '已入住' : '未入住';
      }
    });
  }

  setCheckIn() {
    this.navCtrl.push(SetCheckInPage);
  }

  setCheckOut(check_in_id) {
    this.navCtrl.push(SetCheckOutPage, {check_in_id: check_in_id});
  }

  settlement(check_in_id) {
    this.navCtrl.push(SettlementPage, {check_in_id: check_in_id});
  }

  checkHistory(check_in_id) {
    this.navCtrl.push(HistoryPage, {check_in_id: check_in_id});
  }
}
